package cn.wolfcode.wolf2w.exception;

import cn.wolfcode.wolf2w.util.Consts;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理类
 * 贴上@ControllerAdvice注解,当系统启动时,该类就会被Spring容器加载
 * @ExceptionHandler 表名专门捕捉哪种异常
 */
@ControllerAdvice
public class CommonExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public JsonResult CatchAllException(Exception ex){
        // 打印异常,方便后期维护查看
        ex.printStackTrace();
        return JsonResult.defaultError();
    }

    @ExceptionHandler(LogicException.class)
    @ResponseBody
    public JsonResult CatchLogicException(LogicException le){
        // 打印 LogicException 异常
        le.printStackTrace();
        return JsonResult.error(JsonResult.CODE_ERROR_PARAM,le.getMessage(),null);
    }
}
